from sqlalchemy import Column, Integer, String, Text, DateTime, JSON, Boolean, ForeignKey, Float
from sqlalchemy.orm import relationship
import datetime
from backend.database.database import Base

class DataCleanTask(Base):
    """数据清洗任务表"""
    __tablename__ = "data_clean_tasks"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, comment="任务名称")
    task_name = Column(String(100), nullable=False, comment="任务名称")
    source_type = Column(String(20), nullable=False, comment="数据来源类型：file/database")
    file_path = Column(String(255), nullable=True, comment="文件路径")
    db_config = Column(JSON, nullable=True, comment="数据库配置")
    status = Column(String(20), nullable=False, comment="状态：pending/processing/completed/failed")
    error_message = Column(Text, nullable=True, comment="错误信息")
    created_at = Column(DateTime, default=datetime.datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment="更新时间")
    completed_at = Column(DateTime, nullable=True, comment="完成时间")
    result_file_path = Column(String(255), nullable=True, comment="结果文件路径")
    created_by = Column(String(50), nullable=False, comment="创建用户")

# 品牌配置表
class BrandConfig(Base):
    __tablename__ = "brand_config"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, unique=True)
    category = Column(String(50), nullable=True)  # 品牌类别：食品、电子等
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

# 商品名称排除表
class CommonProductName(Base):
    __tablename__ = "common_product_names"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, unique=True)
    category = Column(String(50), nullable=True)  # 类别：水果、蔬菜等
    description = Column(Text, nullable=True)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

# 单位配置表
class UnitConfig(Base):
    __tablename__ = "unit_config"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(50), nullable=False, unique=True)
    type = Column(String(50), nullable=True)  # 单位类型：重量、数量等
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

# 营销词配置表
class MarketingWordConfig(Base):
    __tablename__ = "marketing_word_config"

    id = Column(Integer, primary_key=True, index=True)
    word = Column(String(100), nullable=False, unique=True, comment="营销词")
    category = Column(String(50), nullable=True, comment="类别：促销词、描述词等")
    is_active = Column(Boolean, default=True, comment="是否启用")
    created_at = Column(DateTime, default=datetime.datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment="更新时间")

    def __repr__(self):
        return f"<MarketingWordConfig {self.word}>"

# 系统配置表
class SystemConfig(Base):
    __tablename__ = "system_config"

    id = Column(Integer, primary_key=True, index=True)
    key = Column(String(100), nullable=False, unique=True)
    value = Column(Text, nullable=True)
    description = Column(Text, nullable=True)
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now) 

# 单位转换规则表
class UnitConversion(Base):
    """单位转换规则表"""
    __tablename__ = "unit_conversion"
    
    id = Column(Integer, primary_key=True, index=True)
    source_unit = Column(String(50), nullable=False, comment="源单位")
    target_unit = Column(String(50), nullable=False, comment="目标单位") 
    conversion_formula = Column(String(255), nullable=False, comment="转换公式，如 'x * 2'")
    description = Column(Text, nullable=True, comment="描述")
    is_active = Column(Boolean, default=True, comment="是否启用")
    created_at = Column(DateTime, default=datetime.datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment="更新时间")
    
    def __repr__(self):
        return f"<UnitConversion {self.source_unit} to {self.target_unit}>" 